package com.kaguya.vams.service.impl;

import com.kaguya.vams.domain.dto.SysMenuDto;
import com.kaguya.vams.domain.entity.SysMenu;
import com.kaguya.vams.domain.entity.SysUser;
import com.kaguya.vams.mapper.SysMenuMapper;
import com.kaguya.vams.mapper.SysUserMapper;
import com.kaguya.vams.service.SysMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kaguya.vams.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author kaguya
 * @since 2021-07-28
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    @Autowired
    private SysUserService userService;

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private SysMenuService menuService;

    /**
     * 获取当前用户的导航菜单信息
     *
     * @return
     */
    @Override
    public List<SysMenuDto> getCurrentUserNav() {
        List<SysMenuDto> dtoList = new ArrayList<>();
        //通过Security上下文获取当前用户名信息
        String username = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        SysUser user = userService.getUserByUserName(username);

        //获取菜单id
        List<Long> menuIds = userMapper.getNavMenuIdByUserId(user.getId());


        if (menuIds.size() > 0) {
            //根据id获取菜单
            List<SysMenu> menuList = menuService.listByIds(menuIds);

            //转树状结构
            menuList = this.buildChildren(menuList);
            //实体转DTO
            dtoList = this.convert(menuList);

        }

        return dtoList;
    }

    /**
     * 菜单信息设置子菜单内容
     *
     * @param menuList
     * @return
     */
    @Override
    public List<SysMenu> buildChildren(List<SysMenu> menuList) {

        List<SysMenu> finalMenus = new ArrayList<>();

        menuList.forEach(e -> {
            //添加子节点
            menuList.forEach(i -> {
                if (i.getParentId() == e.getId()) {
                    e.getChildren().add(i);
                }
            });

            //提取父节点
            if (e.getParentId() == 0L) {
                finalMenus.add(e);
            }
        });

        //finalMenus.forEach(System.out::print);

        return finalMenus;
    }

    /**
     * 实体类转dto
     *
     * @param menuList
     * @return
     */
    @Override
    public List<SysMenuDto> convert(List<SysMenu> menuList) {
        List<SysMenuDto> menuDtoList = new ArrayList<>();

        menuList.forEach(e -> {
            SysMenuDto dto = new SysMenuDto(
                    e.getId(),
                    e.getPerms(),
                    e.getName(),
                    e.getComponent(),
                    e.getIcon(),
                    e.getPath(),
                    e.getStatus(),
                    null
            );
            //设置子节点
            if(e.getChildren().size() > 0) {
                dto.setChildren(convert(e.getChildren()));
            }
            menuDtoList.add(dto);
        });
        return menuDtoList;
    }

    /**
     * 返回带子节点tree结构的菜单列表
     *
     * @return
     */
    @Override
    public List<SysMenu> getMenuList() {
        //获取全部菜单列表
        List<SysMenu> list = list();
        //转换成带子节点的树状结构
        list = this.buildChildren(list);

        return list;
    }
}
